# @Time    :2025/4/17 21:29
# haystack = "sadbutsad"
# needle = "sad"
# m = len(haystack)
# n = len(needle)
# for i in range(m - n + 1):
#     # 检查当前位置开始的子串是否与 needle 匹配
#     if haystack[i] == needle[0] and haystack[i:i + n] == needle:
#         return i
def str(haystack,needle):

    m = len(haystack)
    n = len(needle)
    for i in range(m - n + 1):
       if haystack[i:i + n]==needle[:n]: #haystack[i:i + n]==needle 是跟needle的全部元素进行匹配，haystack[i:i + n]==needle[:n]是跟needle的前n个元素匹配，(不包含索引n)n全部元素进行匹配
           return i
    return -1

haystack = "sadbutsad"
needle = "sad"
res=str(haystack,needle)
print(res)